package com.atsmart.app.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.atsmart.app.R;
import com.atsmart.app.model.ProductInfo;
import com.atsmart.app.model.ProductListInfo;
import com.atsmart.app.widgets.PlaceHolderImageview;

public class ProductDetailAdapter extends LoadingAdapter {
	private ProductListInfo datasource;
	private LayoutInflater inflater;

	public static interface ClickDelegate {
		void onItemClick(int position);
	}

	protected ClickDelegate clickDelegate;

	public ClickDelegate getClickDelegate() {
		return clickDelegate;
	}

	public void setClickDelegate(ClickDelegate delegate) {
		this.clickDelegate = delegate;
	}

	public ProductDetailAdapter(ProductListInfo datasource, Context context) {
		this.datasource = datasource;
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public void appendDataSource(ProductListInfo appendData) {
		datasource.getProducts().addAll(appendData.getProducts());
	}

	private static class ViewHolder {
		PlaceHolderImageview icon;
		TextView name;
		TextView address;
	}

	@Override
	public int getCount() {
		if (datasource.getProducts() == null
				|| datasource.getProducts().size() == 0) {
			return 0;
		}

		if (delegate != null && delegate.needLoading()) {
			return datasource.getProducts().size() + 1;
		} else {
			return datasource.getProducts().size();
		}
	}

	@Override
	public Object getItem(int position) {
		if (datasource.getProducts() == null
				|| position == datasource.getProducts().size()
				&& delegate != null && delegate.needLoading()) {
			return null;
		}
		return datasource.getProducts().get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View loadingRow = super.getView(position, convertView, parent);
		if (loadingRow != null) {
			return loadingRow;
		}
		return getView(position, convertView);
	}

	private View getView(final int position, View convertView) {
		ViewHolder vh;
		if (convertView == null || convertView.getTag() == null) {
			convertView = inflater.inflate(R.layout.product_detail_item_layout, null);
			vh = new ViewHolder();
			vh.icon = (PlaceHolderImageview) convertView
					.findViewById(R.id.img_product_icon);
			vh.name = (TextView) convertView
					.findViewById(R.id.txt_product_name);
			vh.address = (TextView) convertView
					.findViewById(R.id.txt_product_address);
			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}
		ProductInfo clientInfo = datasource.getProducts().get(position);
		vh.icon.setImageUrl(clientInfo.getLogo());
		vh.name.setText(clientInfo.getName());

		vh.address.setText(clientInfo.getDescription());
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				clickDelegate.onItemClick(position);
			}
		});
		return convertView;
	}

	@Override
	protected boolean isLoadingRow(int position) {
		return position >= datasource.getProducts().size();
	}

}
